package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.poi.POIUtils;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Api(tags = "预约设置")
@RestController
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;


    @ApiOperation(value = "查询本年本月预约数据", notes = "查询所有的检查组列表信息")
    @GetMapping("ordersetting/findSettingData/{year}/{month}")
    public Result findSettingData(@PathVariable("year") int year, @PathVariable("month") int month) {
        try {
            Map map = orderSettingService.findSettingData(year, month);
            return new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS, map);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
    }

    @ApiOperation(value = "导入excel数据", notes = "导入excel数据")
    @PostMapping("ordersetting/importOrderSettings")
    public Result importOrderSettings(@RequestParam("excelFile") MultipartFile file) {
        try {
            List<String[]> list = POIUtils.readExcel(file);
            orderSettingService.batchOrderSetting(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @ApiOperation(value = "更改预约最大值", notes = "更改预约最大值")
    @PutMapping("ordersetting/updateNumberByOrderDate/{date}/{number}")
    public Result updateNumberByOrderDate(@PathVariable("date") String date, @PathVariable("number") int number){
        try {
            orderSettingService.updateNumberByOrderDate(date,number);
            return new Result(true, MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_ORDERSETTING_FAIL);
        }
    }


}
